

package de.au.fi.imp.reader;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.openqa.selenium.By;

public class StockNodReader extends WebReader {

	public StockNodReader() {
		getFirefoxDriver();
		driver.get("http://quotes.stocknod.com/stocknod");
		wait(1);
	}

	public List<String> getIsinsForTickers(List<String> tickers) {
		if (tickers == null)
		{
			log.error("input tickers null, returning");
			return null;
		}
		List<String> isins = new ArrayList<String>();
		for (String ticker : tickers) {
			try {
				isins.add(getIsinForTicker(ticker));
			} catch (Exception e) {
				log.error("Could not get isin for ticker {}", ticker);
			}
		}
		if(tickers.size() != isins.size())
		{
			Object[] params = {new Integer(tickers.size()), new Integer(isins.size()), new Integer(tickers.size()-isins.size())};
			log.error("size tickers={} size isins {}, so {} isins could not be found", params);
		}
		return isins;
	}

	public List<String> getIsinsForTickers(String... tickers) {
		if (tickers == null)
			return null;
		return getIsinsForTickers(Arrays.asList(tickers));

	}

	private String getIsinForTicker(String ticker) throws Exception
	{
		if (ticker == null)
			return null;
		setElementTo(By
				.xpath("/html/body/div/div[2]/table/tbody/tr/td/div/div/div/div[3]/div/div/div/input"));
		click();
		element.sendKeys(ticker);
		setElementTo(By
				.xpath("/html/body/div/div[2]/table/tbody/tr/td/div/div/div/div[3]/div/div/div/a"));
		click();
		setElementTo(By
				.xpath("/html/body/div/div[2]/table/tbody/tr/td/div/div[2]/div/div/div[3]/a"));
		click();
		setElementTo(By
				.xpath("/html/body/div/div[2]/table/tbody/tr/td/div/div[2]/table/tbody/tr/td/div/div[3]/table/tbody/tr[4]/td[2]"));
		String isin = logElementText("isin");
		if(isin == null)
			log.error("Could not get isin for ticker {}", ticker);
		wait(2);
		return isin;
	}

}
